var user_check = new Vue({
    el: "#summary",
    data: {
        orderData: ''
    },
    mounted: function () {
        var token=handleLocalStorage("get", "token");
        if (!token){
            alert("请先登录,再结算!");
            window.location.href = "/user/login-register.html";
        }
        this.getCart();
    },
    methods: {
        getCart: function () {
            var cart = handleLocalStorage("get", "cart");
            if (!cart) {
                return false;
            } else {
                axios({
                    url: "/product/cart",
                    method: "post",
                    data: {
                        "cart": cart
                    },
                    headers: {
                        "Content-Type": "application/json"
                    }
                }).then(function (response) {
                    if (response.data.code == 200) {
                        user_check.orderData = response.data.data;
                    } else {
                        window.location.href = "/view/other/404.html";
                    }
                }).catch(function (error) {
                    alert("服务器异常");
                });
            }
        },
        subOrder:function () {
            var value = $('input[name="address"]:checked').val();
            var content=$("#ordernote").val();
            var cart = handleLocalStorage("get", "cart");
            console.log(content);
            if (value==null){
                alert("请先选择地址");
            }else {
                $.ajax({
                    url: "/user/suborder",
                    type: "post",
                    headers: {
                        "Authorization": "Bearer " + handleLocalStorage("get", "token")
                    },
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify({
                        "addressId":parseInt(value),
                        "content":content,
                        "cart":cart
                    }),
                    success: function (response) {
                        if (response.code == 200) {
                            alert("订单提交成功");
                            handleLocalStorage("remove","cart")
                            window.location.href = "/";
                        } else {
                            alert("订单提交失败");
                        }
                    },
                    error: function (error) {
                        alert("服务器错误");
                    }
                });
            }
        }
    }
});
var addressDetail=new Vue({
    el: "#addressDetail",
    data: {
        addresses:''
    },
    mounted: function () {
        this.getAllAddress();

    },
    methods: {
        getAllAddress:function () {
            $.ajax({
                url: "/user/address",
                type: "POST",
                headers: {
                    "Authorization": "Bearer " + handleLocalStorage("get", "token")
                },
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify({
                    "id": -1,
                    "province":"get"
                }),
                success: function (response) {
                    if (response.code == 200) {
                        addressDetail.addresses=response.data;
                    }else {
                        handleLocalStorage("remove","nickname");
                        handleLocalStorage("remove","token");
                        alert(response.message);

                    }
                },
                error: function (error) {
                    alert("服务器错误");
                }
            });
        }
    }
});